home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / edit / pt20pc.zip / SELECT.C < prev    next >
C/C++ Source or Header  |  1991-02-04  |  4KB  |  179 lines

  1. #include "pt.h"
  2. #include "conio.h"
  3.  
  4. /* return 1 only if you see both buttons simultaneously down */
  5. int pascal
  6. /* XTAG:select */
  7. select(w, cp, anchorRow, anchorCol, evhead)
  8.     register struct window *w;
  9.     long cp;
  10.     int anchorRow, anchorCol, evhead;
  11. {
  12.     extern unsigned char msgBuffer[];
  13.     extern unsigned char textBuffer[];
  14.     extern struct window *selWindow;
  15.     extern long selBegin, selEnd;
  16.     extern int selMode;
  17.     extern struct window *pendWindow;
  18.     extern unsigned char scrMapReset;
  19.     extern int debug;
  20.     extern int scrRows;
  21.     extern int doubleClickDelay;
  22.  
  23.     static int buttonDownTime = 0;
  24.     int n, row1, row2, col1, col2, minRow, maxRow;
  25.     long cpLine, cp2, cp3, sb, limit;
  26.  
  27.     /* determine the proper selection mode */
  28.     if( selBegin<=cp && cp<=selEnd && evhead!=-1 && w==selWindow ) {
  29.         row1 = n = GetTime();
  30.         if( n < buttonDownTime )
  31.             buttonDownTime -= 6000;
  32.         n -= buttonDownTime;
  33.         if( n < doubleClickDelay )
  34.             selMode = (selMode+1) & 0x3;
  35.         else
  36.             selMode = SELCHAR;
  37.         buttonDownTime = row1;
  38.     } else {
  39.         selMode = SELCHAR;
  40.         /* remember the time the button went down */
  41.         buttonDownTime = GetTime();
  42.     }
  43.  
  44.  
  45.     /* first erase the current selection */
  46.     if( w != selWindow )
  47.         eraseSelection();
  48.  
  49.     /* set the screen map for the visible parts of this window */
  50.     setMap(w->row1, w->col1, w->row2, w->col2, 1, w->textColor);
  51.     maskTop(w);
  52.  
  53.     /* for now do not reset the screenMap when you write a char */
  54.     /* this avoids frequent recomputation of the screenMap */
  55.     scrMapReset = 1;
  56.  
  57.     /* erase the selection if any part of it is in this window */
  58.     /* otherwise it was erased by `eraseSelection()' above */
  59.     if( w == selWindow 
  60.      && selBegin <= w->posBotline
  61.      && selEnd >= w->posTopline ) {
  62.         sb = selBegin;
  63.         selBegin = selEnd + 1;
  64.         row1 = w->row1 + 1;
  65.         col1 = w->col1 + 1;
  66.         col2 = w->col2 - 1;
  67.         if( selEnd <= w->posBotline )
  68.             limit = selEnd + 1;
  69.         else
  70.             limit = w->posBotline;
  71.         cp2 = w->posTopline;
  72.         minRow = maxRow = -1;
  73.         while( cp2 < limit ) {
  74.             n = 1;
  75.             cp3 = nextLine(w->fileId, cp2, &n);
  76.             if( sb <= cp3 ) {
  77.                 cp3 = fillLine(w, cp2, row1, col1, col2);
  78.                 if( minRow == -1 )
  79.                     minRow = row1;
  80.                 maxRow = row1;
  81.             }
  82.             cp2 = cp3;
  83.             ++row1;
  84.             if( row1 >= w->row2 || cp3 == -1 )
  85.                 break;
  86.         }
  87.         /* remember which rows on the screen to update */
  88.         row1 = minRow;
  89.         row2 = maxRow;
  90.     } else {
  91.         /* set these so no extra rows will be updated */
  92.         row1 = scrRows;
  93.         row2 = -1;
  94.     }
  95.  
  96.     n = -1;
  97.     cpLine = prevLine(w->fileId, cp, &n);
  98.  
  99. /* disable selecting the whole file for now */
  100. if( selMode == SELALL )
  101.     selMode = SELCHAR;
  102.  
  103.     /* Extend the selection as the selection mode requires */
  104.     selWindow = w;
  105.     modeExtend(w, cp, &selBegin, &selEnd);
  106.  
  107.     /* update the display */
  108.     fillLine(w, cpLine, anchorRow, w->col1+1, w->col2-1);
  109.     if( anchorRow < row1 || anchorRow > row2 )
  110.         updateScreen( anchorRow, anchorRow);
  111.     if( row1 <= row2 )
  112.         updateScreen(row1, row2);
  113.  
  114.     /* as long as the button is down, follow the selection */
  115.     if( evhead != -1 ) {    /* only "follow" for regular selections */
  116.         n = followSelection(w, anchorRow, anchorCol, evhead, 0);
  117.     }
  118.  
  119.     /* selecting the end of line should include both CR and LF */
  120.     if( readChar(w->fileId, selBegin) == '\n' ) {
  121.         if( readChar(w->fileId, --selBegin) != '\r' )
  122.             ++selBegin;
  123.     }
  124.  
  125.     /* restore the correct screen map reset value  */
  126.     scrMapReset = 0;
  127.     return n;
  128. }
  129.  
  130. void pascal
  131. /* XTAG:eraseSelection */
  132. eraseSelection()
  133. {
  134.     extern unsigned char msgBuffer[];
  135.     extern struct window *selWindow;
  136.     extern long selBegin, selEnd;
  137.  
  138.     int row1, col1, row2, col2, n;
  139.     int minRow, maxRow;
  140.     long sEnd, cp, cp2;
  141.  
  142.     /* remember selection and then nullify it */
  143.     sEnd = selEnd;
  144.     selEnd = selBegin - 1;
  145.  
  146.     if( selWindow == NULL )
  147.         return;
  148.  
  149.     /* set up the screen mask for the selection window */
  150.     row1 = selWindow->row1 + 1;
  151.     col1 = selWindow->col1 + 1;
  152.     row2 = selWindow->row2 - 1;
  153.     col2 = selWindow->col2 - 1;
  154.     setMap(row1, col1, row2, col2, 1, 0x07);
  155.     maskTop(selWindow);
  156.  
  157.     cp = selWindow->posTopline;
  158.     minRow = maxRow = -1;
  159.     while( row1 <= row2 ) {
  160.         if( cp > sEnd )
  161.             break;
  162.         n = 1;
  163.         cp2 = nextLine(selWindow->fileId, cp, &n);
  164.         if( selBegin <= cp2 ) {
  165.             cp2 = fillLine(selWindow, cp, row1, col1, col2);
  166.             if( minRow == -1 )
  167.                 minRow = row1;
  168.             maxRow = row1;
  169.         }
  170.         if( cp2 == -1 )
  171.             break;
  172.         cp = cp2;
  173.         ++row1;
  174.     }
  175.     if( maxRow != -1 )
  176.         updateScreen(minRow, maxRow);
  177.     selEnd = sEnd;
  178. }
  179.